<?php
namespace Home\Controller;
use Org\Util\String;

class IndexController extends BaseController {
    private $appid = 'wx81ad4a4eb078ee07';
    public function index(){
        $userCurrent = M('user_current_question');
        $openid = session('openid');
        $currentData = $userCurrent->where(array('openid' => $openid))->find();
        $share = '快来参加“应知应会打卡学习”特训，天天打卡，好好学习！';
        if ($currentData['date'] == date('Y-m-d', time()) && $currentData['today_group_count'] != 0){
            $users = M('users');
            $user = $users->where(array('openid' => $openid))->find();
            $map['score'] = array('GT', $user['score']);
            $rank = $users->where($map)->count();
            $rank += 1;
            if ($rank <= 50) {
                $real = $users->order('score desc')->field('nickname, imgurl, score')->limit(50)->select();
                foreach ($real as $key => $value) {
                    if ($value['nickname'] == $user['nickname']) {
                        $rank = $key+1;
                    }
                }
            }
            $share = '我正在参加“应知应会打卡学习”特训，打卡第'.$user['days'].'天，排第'.$rank.'名，明天继续！你也加入吧！';
        }
        $tool = new ToolsController();
        $signature = $tool->JSSDKSignature();
        $this->assign('signature', $signature);
        $this->assign('appid', $this->appid);
        $this->assign('share', $share);
        $this->assign('app', __APP__);
        $this->display();
    }

    public function questions() {
        $isNew = I('post.new') == 'true' ? true : false;
        $openid = session('openid');
        $questions = M('questions');
        $userCurrent = M('user_current_question');
        $currentData = $userCurrent->where(array('openid' => $openid))->find();
        //访问时检查是否为第二天, 重置状态
        if ($currentData['date'] != date('Y-m-d', time())) {
            $currentData['date'] = date('Y-m-d', time());
            $currentData['time'] = time();
            $currentData['current'] = 0;
            $currentData['today_group_count'] = 0;
            $currentData['today_learn_id'] = json_encode(array());
        }
        //访问即打卡
        $users = M('users');
        $user = $users->where(array('openid' => $openid))->find();
        if ($currentData['today_group_count'] == 0 && $currentData['current'] == 0) {
            $user['days'] += 1;
        }
        $users->where(array('openid' => $openid))->save($user);

        //请求新题目时检查时间是否满足
        if (time() - $currentData['time'] < 5 && $isNew) {
            $this->ajaxReturn(array(
                'status' => 403,
                'error'   => '学习时间未满'
            ));
        }
        
        $question_id = $user['phone'];
        if (!$question_id) {
            $question_id = 0;
        }
        $new_question_id = $question_id + 1;
        $question = $questions->where(array('id' => $new_question_id))->find();
        if (!$question) {
            $question = $questions->where(array('id' => 1))->find();
            $new_question_id = 1;
        }
        $user['phone'] = $new_question_id;

        $currentData['current'] += 1;
        $current = $currentData['current'];
        $user['count'] += 1;
        $user['score'] = $user['days']*70 + $user['count']*15;
        $users->where(array('openid' => $openid))->save($user);
        $currentData['time'] = time();
        $currentData['question_id'] = $question['id'];
        $userCurrent->where(array('openid' => $openid))->save($currentData);
        $question['content'] = str_replace(array("\r\n", "\r", "\n"), '<br/>', $question['content']);
        $map['score'] = array('GT', $user['score']);
        $rank = $users->where($map)->count();
        $rank += 1;
        if ($rank <= 50) {
            $real = $users->order('score desc')->field('nickname, imgurl, score')->limit(50)->select();
        }
        foreach ($real as $key => $value) {
            if ($value['nickname'] == $user['nickname']) {
                $rank = $key+1;
            }
        }
        $this->ajaxReturn(array(
            'status' => 200,
            'data'   => array(
                'question' => $question,
                'days' => $user['days'],
                'rank' => $rank,
                'current'  => $current
            )
        ));
    }

    public function rank() {
        $users = M('users');
        $openid = session('openid');
        $user = $users->where(array('openid' => $openid))->find();
        $map['score'] = array('GT', $user['score']);
        $rank = $users->where($map)->count();
        $rank += 1;
        $list = $users->order('score desc')->field('nickname, imgurl, score')->limit(10)->select();
        if ($rank <= 50) {
            $real = $users->order('score desc')->field('nickname, imgurl, score')->limit(50)->select();
        }
        foreach ($real as $key => $value) {
            if ($value['nickname'] == $user['nickname']) {
                $rank = $key+1;
            }
        }
        if ($user['days'] == 0) {
            $rank = '∞';
        }
        $this->ajaxReturn(array(
            'status' => 200,
            'data'   => array(
                'list' => $list,
                'rank' => $rank,
                'nickname' => $user['nickname'],
                'avatar' => $user['imgurl'],
                'days' => $user['days'],
                'groups' => $user['count']
            )
        ));
    }

    public function moreRank() {
        $from = I('post.from');
        $to = I('post.to');
        if (!is_numeric($from) || !is_numeric($to) || $to < $from) {
            $this->ajaxReturn(array(
                'status' => 403,
                'error'  => '参数错误'
            ));
        }
        $offset = $from - 1 >= 0 ? $from - 1:0;
        $limit = $to - $offset;
        $users = M('users');
        $list = $users->order('score desc')->field('nickname, imgurl, score')->limit($offset, $limit)->select();
        $this->ajaxReturn(array(
            'status' => 200,
            'data'  => $list
        ));
    }

}